<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class ColumnController extends AdminbaseController{

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 栏目类型列表
     *
     */
    public function index(){
        $column_id = I("column_id");
        $column_id = 4;
        if(!$column_id){
            $this->error("查找新闻失败！");
        }
        $currentPage = I('param.p', 0);
        if ($currentPage <= 0) {
            $currentPage = 1;
        }

        $pageSize = '20';
        $pageStart = ($currentPage - 1) * $pageSize;
        $pageEnd = $currentPage * $pageSize;
        $typeObj = M("vrnews1.column_type", 'news_');
        $type_list = $typeObj->where(array('column_id' => $column_id, 'status'=>1))
            ->Order("weight desc,create_time desc")->limit($pageStart, $pageSize)->select();
        $count     = $typeObj->where(array('column_id' => $column_id, 'status'=>1))->count();
        $page = $this->page($count ,10);
        $this->assign("page", $page->show('Admin'));
        $this->assign("list", $type_list);
        $this->assign("column_id", $column_id);
        $this->display();
    }

    /**
     * 修改栏目类型信息
     */
    public function editColumnType(){
        $type_id = I("type_id");
        $value   = I("val");
        $type    = I("type");
        if($type == 'name'){
            $data['name'] = $value;
        }else if($type == 'weight'){
            $data['weight'] = $value;
        }
        $typeObj = M("vrnews1.column_type", 'news_');
        echo $typeObj->where(array('type_id' => $type_id))->save($data);exit;
    }

    /**
     * 添加栏目弹窗
     */
    public function addType(){
        $column_id = I("column_id");
        if(!$column_id){
            $this->error("参数错误！");
        }
        $this->assign('column_id',$column_id);
        $this->display('addtype');
    }

    /**
     * 添加栏目
     */
    public function addColumnType(){
        $data['name']   = I("post.name");
        $data['weight'] = I("post.weight");
        $data['column_id'] = I("post.column_id");
        $data['status'] = 1;
        $data['create_time'] = date('Y-m-d H:i:s', time);
        $typeObj = M("vrnews1.column_type", 'news_');
        $type = $typeObj->data($data)->add();
        if($type){
            echo 1;exit;
        }else{
            echo 2;exit;
        }

    }

    /**
     * 删除栏目
     */
    public function del(){
        $type_id = I("type_id");
        if(!$type_id){
            $this->error("参数错误！");
        }
        $data['status'] = 0;
        $typeObj = M("vrnews1.column_type", 'news_');
        $result  = $typeObj->where(array('type_id'=>$type_id))->save($data);
        if ($result) {
            $this->success("删除成功！");
        } else {
            return $this->error($result['Message']);
        }
    }
}